﻿
(function ($) {
    $.fn.ckProcess = function (option) {
        option.obj = this;
        var ck = new ck_process(option);
        ck.initEvent();
        return this;
    }
})(jQuery);

var ck_process = function (option) {
    this.default = {
        obj: null,//已经在初始化的时候赋值option.obj = this;
        items: null,
        itemCallback: null
    };

    this.default = core.extend(this.default, option);

    this.event = new eventManager();
    this.checkAll = this.default.obj;
    this.checkItem = this.default.items;
    var that = this;

    this.event_Map = {
        "all_click": "click_all",
        "item_click": "click_item"
    };

    this.regEvent = function () {
        this.event.addListener(this.event_Map.all_click, this.callback_all);
        this.event.addListener(this.event_Map.item_click, this.callback_item);
        if (typeof this.default.itemCallback == "function") {
            this.event.addListener(this.event_Map.item_click, this.default.itemCallback);
        }
    }

    this.callback_all = function () {
        that.checkItem.each(function () { this.checked = that.checkAll.get(0).checked });
    }

    this.callback_item = function () {
        var o = arguments[0];
        var l = that.checkItem.length;
        if (o.checked == false) {
            that.checkAll.get(0).checked = false;
        } else {
            var bl = true;
            that.checkItem.each(function () {
                if (this.checked == false) {
                    bl = false;
                }
            })

            if (bl == false) {
                that.checkAll.get(0).checked = false;
            }
            else {
                that.checkAll.get(0).checked = true;
            }
        }
    }

    this.initEvent = function () {
        this.regEvent();
        this.checkAll.get(0).addEventListener("click", function () { that.event.doEvent(that.event_Map.all_click); }, false);

        this.checkItem.each(function () {
            this.addEventListener("click", function () { that.event.doEvent(that.event_Map.item_click, this); }, false);
        })
    }
}